const { ApolloServer, gql, SchemaDirectiveVisitor } = require('apollo-server');
const { defaultFieldResolver } = require('graphql');

// Create (or import) a custom schema directive
class UpperCaseDirective extends SchemaDirectiveVisitor {
    visitFieldDefinition(field) {
        const { resolve = defaultFieldResolver } = field;
        console.log('enter');
        field.resolve = async function (...args) {
            const result = await resolve.apply(this, args);
            console.log(result);
            if (typeof result === 'string') {
                return result.toUpperCase();
            }
            return result;
        };
    }
}

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  directive @upper on FIELD_DEFINITION

  type Query {
    hello: String @upper
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
    Query: {
        hello: (parent, args, context) => {
            return 'Hello world!';
        },
    },
};

// Add directive to the ApolloServer constructor
const server = new ApolloServer({
    typeDefs,
    resolvers,
    schemaDirectives: {
        upper: UpperCaseDirective,
    }
});

server.listen().then(({ url }) => {
    console.log(`🚀 Server ready at ${url}`)
});